/*
 * Created by JFormDesigner on Tue Aug 07 22:41:13 CEST 2007
 */

package info.rflow.preferences;

import info.rflow.Utils;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.ResourceBundle;


/**
 * @author Holger Brandl
 */
public class ExecutionModelSettings extends AbstractSettingsPanel {

    public static final String KEEP_FOCUS_IN_RFLOW = "keep.focus";
    public static final boolean KEEP_FOCUS_IN_RFLOW_DEFAULT = false;


    public static final String PUSH_TARGET = "r.push.target";
    public static final String PUSH_TARGET_DEFAULT = getDefaultPushTarget();


    public ExecutionModelSettings() {
        initComponents();

        DefaultComboBoxModel boxModel = new DefaultComboBoxModel();
        for (String pushOption : getEvalTargetOptions()) {
            boxModel.addElement(pushOption);
        }
        pushTargetComboBox.setModel(boxModel);
    }


    private static String[] getEvalTargetOptions() {
        return Utils.isMacOSX() ? new String[]{"R", "R64", "Terminal"} : Utils.isWindowsPlatform() ? new String[]{"R"} : new String[]{""};
    }


    public static String getDefaultPushTarget() {
        return getEvalTargetOptions()[1];
    }


    void resetPanelSettings() {
        Utils.getPrefs().remove(PUSH_TARGET);
        Utils.getPrefs().remove(KEEP_FOCUS_IN_RFLOW);

        loadDefaults();
    }


    void applySettingsChanges() {
        Utils.getPrefs().put(PUSH_TARGET, (String) pushTargetComboBox.getSelectedItem());
        Utils.getPrefs().putBoolean(KEEP_FOCUS_IN_RFLOW, keepFocusCheckBox.isSelected());
    }


    protected void loadDefaults() {
        // load the the settings of this dialog
        pushTargetComboBox.setSelectedItem(Utils.getPrefs().get(PUSH_TARGET, PUSH_TARGET_DEFAULT));
        keepFocusCheckBox.setSelected(Utils.getPrefs().getBoolean(KEEP_FOCUS_IN_RFLOW, KEEP_FOCUS_IN_RFLOW_DEFAULT));

//        numBoxesSpinner.setValue(Utils.getPrefs().getInt(NUM_LEITNER_BOXES, NUM_LEITNER_BOXES_DEFAULT));
//        startBoxSpinner.setValue(Utils.getPrefs().getInt(INIT_LEITNER_BOXES, INIT_LEITNER_BOXES_DEFAULT));
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        ResourceBundle bundle = ResourceBundle.getBundle("info.rflow.translation");
        learnPanel = new JPanel();
        label1 = new JLabel();
        hSpacer1 = new JPanel(null);
        pushTargetComboBox = new JComboBox();
        keepFocusCheckBox = new JCheckBox();
        learnPanel2 = new JPanel();
        checkBox1 = new JCheckBox();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout) getLayout()).columnWidths = new int[]{0, 0};
        ((GridBagLayout) getLayout()).rowHeights = new int[]{0, 0, 0, 0};
        ((GridBagLayout) getLayout()).columnWeights = new double[]{1.0, 1.0E-4};
        ((GridBagLayout) getLayout()).rowWeights = new double[]{0.0, 0.0, 1.0, 1.0E-4};

        //======== learnPanel ========
        {
            learnPanel.setBorder(new CompoundBorder(
                    new TitledBorder(null, bundle.getString("LeitnerSettings.learnPanel.border"), TitledBorder.LEADING, TitledBorder.TOP),
                    new EmptyBorder(5, 5, 5, 5)));
            learnPanel.setLayout(new GridBagLayout());
            ((GridBagLayout) learnPanel.getLayout()).columnWidths = new int[]{0, 0, 0, 0};
            ((GridBagLayout) learnPanel.getLayout()).rowHeights = new int[]{0, 0, 0};
            ((GridBagLayout) learnPanel.getLayout()).columnWeights = new double[]{0.0, 0.0, 0.0, 1.0E-4};
            ((GridBagLayout) learnPanel.getLayout()).rowWeights = new double[]{0.0, 0.0, 1.0E-4};

            //---- label1 ----
            label1.setText(bundle.getString("LeitnerSettings.label1.text"));
            learnPanel.add(label1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));
            learnPanel.add(hSpacer1, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 5), 0, 0));
            learnPanel.add(pushTargetComboBox, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 5, 0), 0, 0));

            //---- keepFocusCheckBox ----
            keepFocusCheckBox.setText(bundle.getString("LeitnerSettings.keepFocusCheckBox.text"));
            learnPanel.add(keepFocusCheckBox, new GridBagConstraints(0, 1, 3, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
        }
        add(learnPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));

        //======== learnPanel2 ========
        {
            learnPanel2.setBorder(new CompoundBorder(
                    new TitledBorder(null, bundle.getString("LeitnerSettings.learnPanel2.border"), TitledBorder.LEADING, TitledBorder.TOP),
                    new EmptyBorder(5, 5, 5, 5)));
            learnPanel2.setLayout(new GridBagLayout());
            ((GridBagLayout) learnPanel2.getLayout()).columnWidths = new int[]{0, 0, 0, 0};
            ((GridBagLayout) learnPanel2.getLayout()).rowHeights = new int[]{0, 0};
            ((GridBagLayout) learnPanel2.getLayout()).columnWeights = new double[]{0.0, 0.0, 0.0, 1.0E-4};
            ((GridBagLayout) learnPanel2.getLayout()).rowWeights = new double[]{0.0, 1.0E-4};

            //---- checkBox1 ----
            checkBox1.setText(bundle.getString("LeitnerSettings.checkBox1.text"));
            learnPanel2.add(checkBox1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));
        }
        add(learnPanel2, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        loadDefaults();


    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JPanel learnPanel;
    private JLabel label1;
    private JPanel hSpacer1;
    private JComboBox pushTargetComboBox;
    private JCheckBox keepFocusCheckBox;
    private JPanel learnPanel2;
    private JCheckBox checkBox1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
